package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IPcProductRuleService;
import com.wondertek.poms.dao.po.PcProductRule;
import com.wondertek.poms.service.util.BeanPropertiesUtil;
import com.wondertek.poms.service.vo.PcProductRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p><b>PcProductRuleController Description:</b> 二级产品规则 Controller层实现</p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Api(value = "PcProductRuleController", description = "二级产品规则 接口")
@RestController
@RequestMapping(value = "/pc-product-rule")
@Slf4j
public class PcProductRuleController extends BaseController {

	@Autowired
	private IPcProductRuleService pcProductRuleService;
	@Autowired
	private BeanPropertiesUtil beanPropertiesUtil;

	/**
	 * 二级产品分页查询
	 *
	 * @param pcProductRuleVo 查询参数接收vo
	 * @param page            页数
	 * @param rows            记录数
	 * @param sidx            排序列
	 * @param sord            排序方式
	 * @return 分页后的规则列表
	 */
	@ApiOperation(value = "二级产品规则 列表", notes = "二级产品规则 列表")
	@GetMapping("/list")
	@ResponseBody
	public ResultBean list(
			PcProductRuleVo pcProductRuleVo,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int rows,
			@RequestParam(defaultValue = "updatedTime") String sidx,
			@RequestParam(defaultValue = "desc") String sord) {
		PcProductRule pcProductRule = new PcProductRule();
		BeanUtils.copyProperties(pcProductRuleVo, pcProductRule);

		Page<PcProductRule> pages = pcProductRuleService
				.findAll(pcProductRule, page, rows, sord, sidx);
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}


	/**
	 * 新增二级产品规则
	 *
	 * @param pcProductRuleVo 二级产品规则vo
	 * @return 新增的二级产品规则
	 */
	@ApiOperation(value = "二级产品规则 新增", notes = "二级产品规则 新增")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productInfoPackageId", required = true, value = "二级产品ID"),
			@ApiImplicitParam(name = "ruleType", required = true, value = "规则类型 1.已选 2.限制"),
			@ApiImplicitParam(name = "copyrightCpid", required = false, value = "版权所属CP"),
			@ApiImplicitParam(name = "copyrightScope", required = false, value = "版权授权范围 1.品牌2.全业务3.咪咕"),
			@ApiImplicitParam(name = "terminal", required = false, value = "版权终端 1.全平台2.手机终端3.PC4.IPTV5.平板6.数字有线网7.互联网电视"),
			@ApiImplicitParam(name = "area", required = false, value = "版权黑名单 1-内地；2-香港；3-澳门；4-台湾；5-海外；6-不限 版权白名单 1-内地；2-香港；3-澳门；4-台湾；5-海外；6-不限；11-江苏；20-广东"),
			@ApiImplicitParam(name = "authorizationWay", required = false, value = "授权方式 1.单片授权2.集体授权3.频道授权4.三方SDK播放授权"),
			@ApiImplicitParam(name = "copyrightType", required = false, value = "版权类型 1.全部2.强版权 3.弱版权"),
			@ApiImplicitParam(name = "digitalCopyrightProtection", required = false, value = "数字版权保护 1.全部 2.是 3.否"),
	})
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean add(@RequestBody PcProductRuleVo pcProductRuleVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(pcProductRuleVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(pcProductRuleVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		PcProductRule pcProductRule = new PcProductRule();
		BeanUtils.copyProperties(pcProductRuleVo, pcProductRule);
		pcProductRule.setDeleted(Constants.DELETE_0);
		PcProductRule.putTimeAndUser(pcProductRule);
		pcProductRuleService.save(pcProductRule);
		return ResultBean.ok(pcProductRule);
	}

	/**
	 * 检查是否重复
	 *
	 * @param pcProductRuleVo 二级产品规则vo
	 * @return
	 */
	private boolean isRepeat(PcProductRuleVo pcProductRuleVo) {
		PcProductRule temp = new PcProductRule();
		CopyUtils.copyProperties(pcProductRuleVo, temp, "id");
		beanPropertiesUtil.setNullIfStringIsBlank(temp);
		temp.setDeleted(Constants.DELETE_0);
		List<PcProductRule> list = pcProductRuleService.findAll(Example.of(temp));
		for (PcProductRule rule : list) {
			PcProductRuleVo vo = new PcProductRuleVo();
			PcProductRuleVo tempVo = new PcProductRuleVo();
			CopyUtils.copyProperties(rule, vo, "id");
			CopyUtils.copyProperties(pcProductRuleVo, tempVo, "id");
			beanPropertiesUtil.setNullIfStringIsBlank(tempVo);
			beanPropertiesUtil.setNullIfStringIsBlank(vo);
			if (vo.equals(tempVo)) {
				if (pcProductRuleVo.getId() == null || !Objects.equals(pcProductRuleVo.getId(), rule.getId())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 删除二级产品呢规则，支持批量删除
	 *
	 * @param ids id以英文逗号连接
	 * @return 删除的对象
	 */
	@ApiOperation(value = "二级产品规则 删除", notes = "二级产品规则 删除")
	@ApiImplicitParam(name = "ids", required = true, dataType = "String", value = "二级产品id拼接字符串，以英文逗号分隔")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] idsArr = ids.split(",");
			for (String id : idsArr) {
				pcProductRuleService.deleteLogic(Long.parseLong(id));
			}
			return ResultBean.ok();
		} else {
			return ResultBean.error();
		}

	}


}
